package com.daxia.ogmall.web.controller.admin;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Drawing;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.util.CellUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.daxia.ogmall.common.Entry;
import com.daxia.ogmall.dto.ExampleDTO;
import com.daxia.ogmall.dto.JsonResultDTO;
import com.daxia.ogmall.service.BaseExcelCallback;
import com.daxia.ogmall.service.ExampleService;
import com.daxia.ogmall.support.Page;
import com.daxia.ogmall.web.controller.BaseController;

@Controller
@RequestMapping(value = "/admin/example", produces="text/html;charset=UTF-8")
public class AdminExampleController extends BaseController {

	@Autowired
	private ExampleService exampleService;

	@RequestMapping(value = "/detail")
	public String detail(Map<String, Object> map, Long id) {
		if (id != null) {
			ExampleDTO dto = exampleService.load(id);
			map.put("n", dto);
		}
		return "admin/example/example_detail";
	}
	
	@ResponseBody
	@RequestMapping(value = "/save")
	
	public String save(ExampleDTO dto, JsonResultDTO resultDTO) throws Exception {
		if (dto.getId() == null) {
			exampleService.create(dto);
		} else {
			exampleService.updateAllFields(dto);
		}
		return okAndClose(resultDTO);
	}

    
	@ResponseBody
	@RequestMapping(value = "/delete") 
	public String delete(Long[] ids, JsonResultDTO resultDTO) throws IOException {
		exampleService.deleteByIds(ids);
		return okAndRefresh(resultDTO);
	}
	
	@RequestMapping(value = "/list")
	
	public String list(ExampleDTO dto, Map<String, Object> map, Page page) {
		List<ExampleDTO> dtos = exampleService.find(dto, page);
		map.put("list", dtos);
		map.put("query", dto);
		return "admin/example/example_list";
	}
	
	@ResponseBody
	@RequestMapping(value = "/datagrid")
    
    public String datagrid(ExampleDTO dto, Map<String, Object> map, Page page) throws Exception {
        List<ExampleDTO> dtos = exampleService.find(dto, page);
        JSONArray jsonArray = new JSONArray();
        for (ExampleDTO example : dtos) {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("id", example.getId());
            jsonObject.put("username", example.getUsername());
			jsonObject.put("createDate", formatDate(example.getCreateDate(), "yyyy-MM-dd"));	            	
            jsonObject.put("myname", example.getMyname());
            jsonArray.add(jsonObject);
        }
        JSONObject json = new JSONObject();
        json.put("total", page.getTotalRecords());
        json.put("rows", jsonArray);
        return json.toJSONString();
    }
	
	@ResponseBody
	@RequestMapping(value = "/expExcel")
	public void expExcel(ExampleDTO dto, Map<String, Object> map, Page page) throws IOException {
		List<ExampleDTO> dtos = exampleService.find(dto, page);
		Entry<String, String> excelEntry = toExcel("d://", "个人信息列表", dtos, new BaseExcelCallback<ExampleDTO>() {

			@Override
			public Row header(Sheet sheet) {
				Workbook wb = sheet.getWorkbook();
				CellStyle style = buildHeaderStyle(wb);
				Row row = sheet.createRow(0);
				int column = 0;
				String[] titles = new String[] { "id", "姓名", "时间"};
				for (String title : titles) {
					CellUtil.createCell(row, column, title, style);
					column += 1;
				}
				return row;
			}

			@Override
			public Row convert(Row row, ExampleDTO obj) {
				createCell(row, 0, obj.getId());
				createCell(row, 1, StringUtils.trimToEmpty(obj.getUsername()));
				createCell(row, 2, obj.getCreateDate());
				return row;
			}

			@Override
			public Row convert(Workbook wb, Drawing pa, Row row, ExampleDTO obj) {
				// TODO Auto-generated method stub
				return null;
			}

			@Override
			public Row footer(Sheet sheet) {
				// TODO Auto-generated method stub
				return null;
			}
		});
		List<Entry<String, String>> files = new ArrayList<Entry<String, String>>();
		files.add(excelEntry);
	}
}
